package cn.zxf.note.note.note;

import cn.zxf.note.common.web.AbstractController;
import cn.zxf.note.note.note.dto.NoteItemDto;
import cn.zxf.note.note.note.dto.NoteSingleLineDto;
import com.github.zengxf.common.domain.PageResult;
import com.github.zengxf.common.domain.SimpleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;

import java.util.Map;

/**
 * Created by zengxf on 2019/4/2.
 */
@RestController
@RequestMapping("/api/note/note")
public class NoteController extends AbstractController {

    @Autowired
    private NoteService service;

    @GetMapping("find-one/{noteId}")
    public NoteItemDto findOne(
            ServerWebExchange ex,
            @PathVariable Integer noteId
    ) {
        return service.findOne(super.curUserId(ex), noteId);
    }

    @PostMapping("save")
    public NoteItemDto save(
            ServerWebExchange ex,
            @RequestBody NoteItemDto dto
    ) {
        return service.save(super.curUserId(ex), dto);
    }

    @DeleteMapping("delete/{noteId}")
    public NoteItemDto delete(
            ServerWebExchange ex,
            @PathVariable Integer noteId
    ) {
        return service.delete(super.curUserId(ex), noteId);
    }

    @GetMapping("find-by-type")
    public PageResult<NoteSingleLineDto> findByType(
            ServerWebExchange ex,
            @RequestParam Integer typeId,
            @RequestParam(defaultValue = "0") Integer pageIndex,
            @RequestParam(defaultValue = "20") Integer pageSize
    ) {
        return service.findByType(super.curUserId(ex), typeId, pageIndex, pageSize);
    }

    @GetMapping("search")
    public PageResult<NoteSingleLineDto> search(
            ServerWebExchange ex,
            @RequestParam(defaultValue = "-1") Integer type,
            @RequestParam(defaultValue = "") String keyword,
            @RequestParam(defaultValue = "0") Integer pageIndex,
            @RequestParam(defaultValue = "20") Integer pageSize
    ) {
        return service.search(super.curUserId(ex), type, keyword, pageIndex, pageSize);
    }

    @PostMapping("import-notes")
    public SimpleResult importNotes(
            ServerWebExchange ex,
            @RequestBody Map<String, String> data
    ) {
        String content = data.get("content");
        service.importNotes(super.curUserId(ex), content);
        return SimpleResult.OK;
    }

    @PutMapping("transfer-notes")
    public SimpleResult transferNotes(
            ServerWebExchange ex,
            @RequestBody Map<String, Integer> data
    ) {
        Integer fromTypeId = data.get("fromTypeId");
        Integer toTypeId = data.get("toTypeId");
        service.transferNotes(super.curUserId(ex), fromTypeId, toTypeId);
        return SimpleResult.OK;
    }

}
